{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "3859a157",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'refresh_token': '25.7564141639186804ff7c0c92604e13e6.315360000.1964432512.282335-25904452', 'expires_in': 2592000, 'session_key': '9mzdA5LfGgRPwP+HNY2bTosnUxr62jb9H13ybg58XTK6uUdXuTzIdYGwfKByYxcWtWPNoGDGdq+COSyLXi44SnUfxtdbbQ==', 'access_token': '24.88f9c3299ec624055a69cf2c27f56b10.2592000.1651664512.282335-25904452', 'scope': 'public vis-classify_dishes vis-classify_car brain_all_scope vis-classify_animal vis-classify_plant brain_object_detect brain_realtime_logo brain_dish_detect brain_car_detect brain_animal_classify brain_plant_classify brain_ingredient brain_advanced_general_classify brain_custom_dish brain_poi_recognize brain_vehicle_detect brain_redwine brain_currency brain_vehicle_damage brain_multi_ object_detect wise_adapt lebo_resource_base lightservice_public hetu_basic lightcms_map_poi kaidian_kaidian ApsMisTest_Test权限 vis-classify_flower lpq_开放 cop_helloScope ApsMis_fangdi_permission smartapp_snsapi_base smartapp_mapp_dev_manage iop_autocar oauth_tp_app smartapp_smart_game_openapi oauth_sessionkey smartapp_swanid_verify smartapp_opensource_openapi smartapp_opensource_recapi fake_face_detect_开放Scope vis-ocr_虚拟人物助理 idl-video_虚拟人物助理 smartapp_component smartapp_search_plugin avatar_video_test b2b_tp_openapi b2b_tp_openapi_online', 'session_secret': 'afc5e1b3a62b19b258ae25cee94bd2c5'}\n"
     ]
    }
   ],
   "source": [
    "\n",
    "# encoding:utf-8\n",
    "import requests \n",
    "\n",
    "# client_id 为官网获取的AK， client_secret 为官网获取的SK\n",
    "host = 'https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id=FT6vzMxsZEgdIAwa6CkEIPIK&client_secret=CBrtoKUsQBwL23e12tWztGmntj0mevoZ'\n",
    "response = requests.get(host)\n",
    "if response:\n",
    "    print(response.json())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "31e2dc82",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'24.88f9c3299ec624055a69cf2c27f56b10.2592000.1651664512.282335-25904452'"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "Access_Token = response.json()['access_token']\n",
    "Access_Token"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "e8c2dc74",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'result_num': 5, 'result': [{'keyword': '树', 'score': 0.725525, 'root': '植物-树', 'baike_info': {'baike_url': 'http://baike.baidu.com/item/%E6%A0%91/2699484', 'image_url': 'https://bkimg.cdn.bcebos.com/pic/6159252dd42a2834218a2c2154b5c9ea15cebfef', 'description': '树是一种数据结构，它是由n(n≥0)个有限节点组成一个具有层次关系的集合。把它叫做“树”是因为它看起来像一棵倒挂的树，也就是说它是根朝上，而叶朝下的。它具有以下的特点：每个节点有零个或多个子节点；没有父节点的节点称为根节点；每一个非根节点有且只有一个父节点；除了根节点外，每个子节点可以分为多个不相交的子树。'}}, {'keyword': '果林', 'score': 0.563555, 'root': '植物-植物园'}, {'keyword': '石榴树', 'score': 0.408016, 'root': '植物-其它'}, {'keyword': '生态林', 'score': 0.253892, 'root': '植物-植物园'}, {'keyword': '山峦', 'score': 0.068985, 'root': '自然风景-山峦'}], 'log_id': 1510957180656086326}\n"
     ]
    }
   ],
   "source": [
    "# encoding:utf-8\n",
    "import base64\n",
    "\n",
    "'''\n",
    "通用物体和场景识别\n",
    "'''\n",
    "\n",
    "request_url = \"https://aip.baidubce.com/rest/2.0/image-classify/v2/advanced_general\"\n",
    "# 二进制方式打开图片文件\n",
    "f = open('tree.jpg', 'rb')\n",
    "img = base64.b64encode(f.read())\n",
    "\n",
    "payload={\n",
    "    'access_token':Access_Token,\n",
    "    'image':img,\n",
    "    'baike_num':1\n",
    "}\n",
    "\n",
    "headers = {'content-type': 'application/x-www-form-urlencoded'}\n",
    "response = requests.post(request_url, data=payload, headers=headers)\n",
    "if response:\n",
    "    print (response.json())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "071757de",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
